/****************************************************************************
** Takes basic datafile and interprets variables for Analysis				*
**																			*
** Produces three files that will be needed for later analysis				*
** 1) econographicsTransformed.dta: base dataset for summary statistics		*
** 2) econographicsForPCA: renames some variables for ease of use			*
** 		in computing the big correlation table								*
** 3) econographicsTransformedWithComponents.dta: this standardizes 		*
** 	 	the various variables that are needed for factor analyses			*	
**																			*
** Produced by Erik Snowberg, October 13, 2022								*
** 		Based on Code by Jonathan Chapman and Erik Snowberg					*
****************************************************************************/

use econographics, clear

/*************************************************************************
* Probability weighting and common ratio
*************************************************************************/
// Comparison with sure value
gen riskAversionCRSure1Temp=(probWeightMPL1Min+probWeightMPL1Max)/2
gen riskAversionCRSure2Temp=(probWeightMPL2Min+probWeightMPL2Max)/2

replace riskAversionCRSure1Temp=2500 if probWeightMPL1Min==2200
replace riskAversionCRSure2Temp=4000 if probWeightMPL2Min==3600

gen riskAversionCRSure1=(0.8*riskAversionCRSure1Temp-2500)/(0.8*riskAversionCRSure1Temp)
gen riskAversionCRSure2=(0.75*riskAversionCRSure2Temp-4000)/(0.75*riskAversionCRSure2Temp)

// Choice over lotteries
gen riskAversionCRLottery1Temp=(probWeightMPL3Min+probWeightMPL3Max)/2
gen riskAversionCRLottery2Temp=(probWeightMPL4Min+probWeightMPL4Max)/2

replace riskAversionCRLottery1Temp=2500 if probWeightMPL3Min==2200
replace riskAversionCRLottery2Temp=4000 if probWeightMPL4Min==3600

gen riskAversionCRLottery1=(0.2*riskAversionCRLottery1Temp-0.25*2500)/(0.2*riskAversionCRLottery1Temp)
gen riskAversionCRLottery2=(0.15*riskAversionCRLottery2Temp-0.2*4000)/(0.15*riskAversionCRLottery2Temp)

// Generate common ratio
gen commonRatio1 = riskAversionCRSure1 - riskAversionCRLottery1
gen commonRatio2 = riskAversionCRSure2 - riskAversionCRLottery2

// Version for analysis (design differently so not a non-linear transformation)
gen FAriskAversionCRSure1=(0.8*riskAversionCRSure1Temp-2500)/2500
gen FAriskAversionCRSure2=(0.75*riskAversionCRSure2Temp-4000)/4000

gen FAriskAversionCRLottery1=(0.2*riskAversionCRLottery1Temp-0.25*2500)/(0.25*2500)
gen FAriskAversionCRLottery2=(0.15*riskAversionCRLottery2Temp-0.2*4000)/(0.2*4000)

label var riskAversionCRSure1 "Risk aversion from 2500 vs 80/20 lottery. Higher=more risk averse (units=% of EV of lottery)"
label var riskAversionCRSure2 "Risk aversion from 4000 vs 75/25 lottery. Higher=more risk averse (units=% of EV of lottery)"
label var riskAversionCRLottery1 "Risk aversion from choice between 25/20 and 20/80 lotteries (units=% of EV of lottery)"
label var riskAversionCRLottery2 "Risk aversion from choice between 15/85 and 20/80 lotteries (units=% of EV of lottery)"

label var FAriskAversionCRSure1 "Risk aversion from 2500 vs 80/20 lottery. Higher=more risk averse (units=% of EV of lottery)"
label var FAriskAversionCRSure2 "Risk aversion from 4000 vs 75/25 lottery. Higher=more risk averse (units=% of EV of lottery)"
label var FAriskAversionCRLottery1 "Risk aversion from choice between 25/20 and 20/80 lotteries (units=% of EV of lottery)"
label var FAriskAversionCRLottery2 "Risk aversion from choice between 15/85 and 20/80 lotteries (units=% of EV of lottery)"
label var commonRatio1 "Common Ratio from 80/20 sure and vs 80/20-25/75 lotteries"
label var commonRatio2 "Common Ratio from 75/25 sure and vs 80/20-15/85 lotteries"

*************************************************************************
** WTA, WTP and endowment effect
*************************************************************************/	
// Convert into midpoints of MPLs
gen WTA1Temp=(WTACertaintyMPL1Min+WTACertaintyMPL1Max)/2
gen WTA2Temp=(WTACertaintyMPL2Min+WTACertaintyMPL2Max)/2
gen WTP1Temp=(WTPCertaintyMPL1Min+WTPCertaintyMPL1Max)/2
gen WTP2Temp=(WTPCertaintyMPL2Min+WTPCertaintyMPL2Max)/2

replace WTA1Temp=1000 if WTACertaintyMPL1Min==0
replace WTA1Temp=9000 if WTACertaintyMPL1Max==10000
replace WTA2Temp=2000 if WTACertaintyMPL2Min==1500
replace WTA2Temp=8000 if WTACertaintyMPL2Max==9000

replace WTP1Temp=1000 if WTPCertaintyMPL1Min==0
replace WTP1Temp=9000 if WTPCertaintyMPL1Max==10000
replace WTP2Temp=2000 if WTPCertaintyMPL2Min==1500
replace WTP2Temp=8000 if WTPCertaintyMPL2Max==9000

gen WTA1=WTA1Temp/5000
gen WTA2=WTA2Temp/5000
gen WTP1=WTP1Temp/5000
gen WTP2=WTP2Temp/5000

// Endowment effect
gen endowmentEffect1=WTA1-WTP1
gen endowmentEffect2=WTA2-WTP2

label var WTA1 "Willingness to Accept 1 as% of EV (note higher=more risk loving)"
label var WTA2 "Willingness to Accept 2 as% of EV (note higher=more risk loving)"
label var WTP1 "Willingness to Pay 1 as% of EV (note higher=more risk loving)"
label var WTP2 "Willingness to Pay 2 as% of EV (note higher=more risk loving)"
label var endowmentEffect1 "WTA1-WTP1"
label var endowmentEffect2 "WTA2-WTP2"

/*************************************************************************
* Time preference
*************************************************************************/	
// Convert MPLs using midpoints , generating dummy variables when all one side is chosen
gen patience1Temp=(timeDiscountMPL1Min+timeDiscountMPL1Max)/2
replace patience1Temp=4000 if timeDiscountMPL1Min==4000
replace patience1Temp=4000 if timeDiscountMPL1Min==4100
gen patience2Temp=(timeDiscountMPL2Min+timeDiscountMPL2Max)/2
replace patience2Temp=6000 if timeDiscountMPL2Min==6000
replace patience2Temp=6000 if timeDiscountMPL2Min==6100

gen patience1= 1-((4000-patience1Temp)/4000)/1.5
gen patience2= 1-((6000-patience2Temp)/6000)/3

// Estimate discount rate (delta) from 1st MPL
gen discountRate1=(patience1Temp/4000)^(2/3)
label var discountRate1 "Discount rate parameter from first time preference MPL"

// Estimate discount rate (delta) from 2nd MPL
gen discountRate2=(patience2Temp/6000)^(1/3)
label var discountRate2 "Discount rate parameter from second time preference MPL"

label var patience1 "Linear discount rate for choice compared to 4000 in 45 days"
label var patience2 "Linear discount rate for choice compared to 6000 in 90 days"

/*************************************************************************
* Risk aversion (III.P)
*************************************************************************/
// Convert into midpoints of MPLs (note: higher=more risk loving)
gen riskAversionUrn1Temp=(riskAversionMPL1Min+riskAversionMPL1Max)/2
gen riskAversionUrn2Temp=(riskAversionMPL2Min+riskAversionMPL2Max)/2

foreach i in D_riskAversionUrn {
	gen `i'1Low=0
	gen `i'1High=0
	gen `i'2Low=0
	gen `i'2High=0
} 

replace D_riskAversionUrn1High=1 if riskAversionMPL1Min==-1000
replace D_riskAversionUrn1Low=1 if riskAversionMPL1Max==12000
replace D_riskAversionUrn2High=1 if riskAversionMPL2Min==-1000
replace D_riskAversionUrn2Low=1 if riskAversionMPL2Max==9000
replace riskAversionUrn1Temp=0 if D_riskAversionUrn1High==1
replace riskAversionUrn1Temp=10000 if D_riskAversionUrn1Low==1
replace riskAversionUrn2Temp=0 if D_riskAversionUrn2High==1
replace riskAversionUrn2Temp=8000 if D_riskAversionUrn2Low==1

gen riskAversionUrn1=1-riskAversionUrn1Temp/(0.5*10000)
gen riskAversionUrn2=1-riskAversionUrn2Temp/(0.5*8000)
label var riskAversionUrn1 "In % of EV of gamble over 0/10000 (=1- CE/0.5*prize). Higher=risk averse"
label var riskAversionUrn2 "In % of EV of gamble over 0/8000 (=1- CE/0.5*prize). Higher=risk averse"

/*************************************************************************
* Risk aversion (III.Q)
*************************************************************************/
// Convert into midpoints of MPLs (note: higher=more risk loving)
gen riskAversionGain1Temp=(riskAversionMPL3Min+riskAversionMPL3Max)/2
gen riskAversionGain2Temp=(riskAversionMPL4Min+riskAversionMPL4Max)/2

foreach i in D_riskAversionGain {
	gen `i'1Low=0
	gen `i'1High=0
	gen `i'2Low=0
	gen `i'2High=0
} 

replace D_riskAversionGain1High=1 if riskAversionMPL3Min==-500
replace D_riskAversionGain1Low=1 if riskAversionMPL3Max==5500
replace D_riskAversionGain2High=1 if riskAversionMPL4Min==600
replace D_riskAversionGain2Low=1 if riskAversionMPL4Max==4600
replace riskAversionGain1Temp=0 if D_riskAversionGain1High==1
replace riskAversionGain1Temp=5000 if D_riskAversionGain1Low==1
replace riskAversionGain2Temp=1000 if D_riskAversionGain2High==1
replace riskAversionGain2Temp=4000 if D_riskAversionGain2Low==1

gen riskAversionGain1=1-riskAversionGain1Temp/(2500)
gen riskAversionGain2=1-riskAversionGain2Temp/(2500)
label var riskAversionGain1 "In % of EV of gamble over 0/5000 (=1- CE/EV). Higher=risk averse"
label var riskAversionGain2 "In % of EV of gamble over 1000/4000 (=1- CE/EV). Higher=risk averse"

/*************************************************************************
* Risk aversion in mixed domain (III.R.1 and 2)
*************************************************************************/
// Convert into midpoints of MPLs and into gain domain
gen riskAversionMixGainLoss1Temp=10000+(lossAversionMPL1Min+lossAversionMPL1Max)/2
gen riskAversionMixGainLoss2Temp=10000+(lossAversionMPL2Min+lossAversionMPL2Max)/2

foreach i in D_riskAversionMixGainLoss {
	gen `i'1Low=0
	gen `i'1High=0
	gen `i'2Low=0
	gen `i'2High=0
} 

replace D_riskAversionMixGainLoss1High=1 if lossAversionMPL1Min==-6000
replace D_riskAversionMixGainLoss1Low=1 if lossAversionMPL1Max==7000
replace D_riskAversionMixGainLoss2High=1 if lossAversionMPL2Min==-5000
replace D_riskAversionMixGainLoss2Low=1 if lossAversionMPL2Max==5000
replace riskAversionMixGainLoss1Temp=-5000+10000 if D_riskAversionMixGainLoss1High==1
replace riskAversionMixGainLoss1Temp=5000+10000 if D_riskAversionMixGainLoss1Low==1
replace riskAversionMixGainLoss2Temp=-4000+10000 if D_riskAversionMixGainLoss2High==1
replace riskAversionMixGainLoss2Temp=4000+10000 if D_riskAversionMixGainLoss2Low==1

gen riskAversionMixGainLoss1=1-riskAversionMixGainLoss1Temp/10000
gen riskAversionMixGainLoss2=1-riskAversionMixGainLoss2Temp/10000

label var riskAversionMixGainLoss1 "In % of EV of gamble over 5k/-5k (=1- CE/EV*). Higher=risk averse"
label var riskAversionMixGainLoss2 "In % of EV of gamble over 4k/-4k  (=1- CE/EV*). Higher=risk averse"

/*************************************************************************
* Risk aversion in loss domain (III.R.3 and 4)
*************************************************************************/
// Convert into midpoints of MPLs (note: higher=less loss averse)
gen riskAversionLoss1Temp=(lossAversionMPL3Min+lossAversionMPL3Max)/2
gen riskAversionLoss2Temp=(lossAversionMPL4Min+lossAversionMPL4Max)/2

foreach i in D_riskAversionLoss {
	gen `i'1Low=0
	gen `i'1High=0
	gen `i'2Low=0
	gen `i'2High=0
} 

replace D_riskAversionLoss1High=1 if lossAversionMPL3Min==-5500
replace D_riskAversionLoss1Low=1 if lossAversionMPL3Max==500
replace D_riskAversionLoss2High=1 if lossAversionMPL4Min==-4400
replace D_riskAversionLoss2Low=1 if lossAversionMPL4Max==-600
replace riskAversionLoss1Temp=-5000 if D_riskAversionLoss1High==1
replace riskAversionLoss1Temp=0 if D_riskAversionLoss1Low==1
replace riskAversionLoss2Temp=-4000 if D_riskAversionLoss2High==1
replace riskAversionLoss2Temp=-1000 if D_riskAversionLoss2Low==1

gen riskAversionLoss1=-(1-riskAversionLoss1Temp/-2500)
gen riskAversionLoss2=-(1-riskAversionLoss2Temp/-2500)

label var riskAversionLoss1 "In % of EV of gamble over -5k/0 (=-(1- CE/EV). Higher=risk averse"
label var riskAversionLoss2 "In % of EV of gamble over -4k/-1k  (=-(1- CE/EV)). Higher=risk averse"

/*************************************************************************
* Compound urns (III.S.)
*************************************************************************/

// Convert into midpoints of MPLs (note: higher=more risk averse)
gen compoundLotteryAttitude1Temp  =(compoundUrnsMPL1Min+compoundUrnsMPL1Max)/2
gen compoundLotteryAttitude2Temp  =(compoundUrnsMPL2Min+compoundUrnsMPL2Max)/2

foreach i in D_compoundLotteryAttitude {
	gen `i'1Low=0
	gen `i'1High=0
	gen `i'2Low=0
	gen `i'2High=0
} 

replace D_compoundLotteryAttitude1High=1 if compoundUrnsMPL1Min==-1000
replace D_compoundLotteryAttitude1Low=1 if compoundUrnsMPL1Max==12000
replace D_compoundLotteryAttitude2High=1 if compoundUrnsMPL2Min==-1000
replace D_compoundLotteryAttitude2Low=1 if compoundUrnsMPL2Max==9000
replace compoundLotteryAttitude1Temp=0 if D_compoundLotteryAttitude1High==1
replace compoundLotteryAttitude1Temp=10000 if D_compoundLotteryAttitude1Low==1
replace compoundLotteryAttitude2Temp=0 if D_compoundLotteryAttitude2High==1
replace compoundLotteryAttitude2Temp=8000 if D_compoundLotteryAttitude2Low==1

gen compoundLotteryAttitude1=1-compoundLotteryAttitude1Temp/(0.5*10000)
gen compoundLotteryAttitude2=1-compoundLotteryAttitude2Temp/(0.5*8000)

** Compound lotteries
gen compoundAversion1 = compoundLotteryAttitude1 - riskAversionUrn1
gen compoundAversion2 = compoundLotteryAttitude2 - riskAversionUrn2

label var compoundLotteryAttitude1 "Compound urn, % of EV of gamble"
label var compoundLotteryAttitude2 "Compound urn, % of EV of gamble"
label var compoundAversion1 	"Compound lottery adjusted for risk aversion, % of EV of lottery"
label var compoundAversion2 	"Compound lottery adjusted for risk aversion, % of EV of lottery" 

/*************************************************************************
* Ambiguity aversion
*************************************************************************/	
// Convert into midpoints of MPLs	
gen ambiguityAttitude1Temp=(ambAversionMPL1Min+ambAversionMPL1Max)/2
gen ambiguityAttitude2Temp=(ambAversionMPL2Min+ambAversionMPL2Max)/2

foreach i in D_ambiguityAttitude {
	gen `i'1Low=0
	gen `i'1High=0
	gen `i'2Low=0
	gen `i'2High=0
} 

replace D_ambiguityAttitude1High=1 if ambAversionMPL1Min==-1000
replace D_ambiguityAttitude1Low=1 if ambAversionMPL1Max==12000
replace D_ambiguityAttitude2High=1 if ambAversionMPL2Min==-1000
replace D_ambiguityAttitude2Low=1 if ambAversionMPL2Max==9000
replace ambiguityAttitude1Temp=0 if D_ambiguityAttitude1High==1
replace ambiguityAttitude1Temp=10000 if D_ambiguityAttitude1Low==1
replace ambiguityAttitude2Temp=0 if D_ambiguityAttitude2High==1
replace ambiguityAttitude2Temp=8000 if D_ambiguityAttitude2Low==1

gen ambiguityAttitude1=1-ambiguityAttitude1Temp/(0.5*10000)
gen ambiguityAttitude2=1-ambiguityAttitude2Temp/(0.5*8000)

** Ambiguity aversion adjusting for risk
gen ambiguityAversion1 = ambiguityAttitude1 - riskAversionUrn1
gen ambiguityAversion2 = ambiguityAttitude2 - riskAversionUrn2

label var ambiguityAttitude1 "Higher=more ambiguity averse. % of EV of gamble assuming 50:50 beliefs"
label var ambiguityAttitude2 "Higher=less ambiguity averse. % of EV of gamble assuming 50:50 beliefs"
label var ambiguityAversion1  "Ambiguity aversion, adjusted for risk preferences, % of EV of lottery"
label var ambiguityAversion2  "Ambiguity aversion, adjusted for risk preferences, % of EV of lottery"

/*************************************************************************
* Distributional preferences
*************************************************************************/	
gen disadInequalityAversion1Temp=(distPreferenceMPL1Min+distPreferenceMPL1Max)/2
replace disadInequalityAversion1Temp=1600 if disadInequalityAversion1Temp==.&distPreferenceMPL1Min==.
replace disadInequalityAversion1Temp=6400 if disadInequalityAversion1Temp==.&distPreferenceMPL1Max==.

gen disadInequalityAversion2Temp=(distPreferenceMPL3Min+distPreferenceMPL3Max)/2
replace disadInequalityAversion2Temp=3400 if disadInequalityAversion2Temp==.&distPreferenceMPL3Min==.
replace disadInequalityAversion2Temp=10600 if disadInequalityAversion2Temp==.&distPreferenceMPL3Max==.

gen advanInequalityAversion1Temp=(distPreferenceMPL2Min+distPreferenceMPL2Max)/2
replace advanInequalityAversion1Temp=1600 if advanInequalityAversion1Temp==.&distPreferenceMPL2Min==.
replace advanInequalityAversion1Temp=6400 if advanInequalityAversion1Temp==.&distPreferenceMPL2Max==.

gen advanInequalityAversion2Temp=(distPreferenceMPL4Min+distPreferenceMPL4Max)/2
replace advanInequalityAversion2Temp=3400 if advanInequalityAversion2Temp==.&distPreferenceMPL4Min==.
replace advanInequalityAversion2Temp=10600 if advanInequalityAversion2Temp==.&distPreferenceMPL4Max==.

gen disadInequalityAversion1=(disadInequalityAversion1Temp-4000)/disadInequalityAversion1Temp
gen disadInequalityAversion2=(disadInequalityAversion2Temp-7000)/disadInequalityAversion2Temp
gen advanInequalityAversion1=(advanInequalityAversion1Temp-4000)/advanInequalityAversion1Temp
gen advanInequalityAversion2=(advanInequalityAversion2Temp-7000)/advanInequalityAversion2Temp

label var disadInequalityAversion1 "% of income prepared to forgo to get equal split  (disadvantageous inequality)"
label var disadInequalityAversion2 "% of income prepared to forgo to get equal split  (disadvantageous inequality)"
label var advanInequalityAversion1 "% of income prepared to forgo to get equal split (advantageous inequality)"
label var advanInequalityAversion2  "% of income prepared to forgo to get equal split (advantageous inequality)"

gen FAdisadInequalityAversion1=(disadInequalityAversion1Temp-4000)/4000
gen FAdisadInequalityAversion2=(disadInequalityAversion2Temp-7000)/7000
gen FAadvanInequalityAversion1=(advanInequalityAversion1Temp-4000)/4000
gen FAadvanInequalityAversion2=(advanInequalityAversion2Temp-7000)/7000

gen directionAdvanInequalAversion1=1
gen directionAdvanInequalAversion2=1
gen directionDisadInequalAversion1=1
gen directionDisadInequalAversion2=1

/*************************************************************************
* Strategic sophistication=100-number
*************************************************************************/
replace strategicSophistication1=100-strategicSophistication1
replace strategicSophistication2=100-strategicSophistication2
label var strategicSophistication1 "100-answer to strat.sophistication with 10 players"
label var strategicSophistication2 "100-answer to strat.sophistication with 5 players"

/*************************************************************************
* Normalize altruism reciprocity and punishment
*************************************************************************/
gen altruism=dictator/6000
label var altruism "% of max possible points sent to other person"
gen trust=trustSent/6000
label var trust "% of max possible points sent to other person in trust game"
replace punishSender=punishSender/2000
replace punishReceiver=punishReceiver/2000
label var punishSender "% of max possible points used to punish sender of full amount"
label var punishReceiver "% max possible points used to punish person who sent back 0 when sent full amount"
	
gen reciprocityTemp1000=trustSentBack1000/2000	
gen reciprocityTemp2000=trustSentBack2000/4000
gen reciprocityTemp3000=trustSentBack3000/6000
gen reciprocityTemp4000=trustSentBack4000/8000
gen reciprocityTemp5000=trustSentBack5000/10000
gen reciprocityTemp6000=trustSentBack6000/12000

egen reciprocityLevel=rowmean(reciprocityTemp1000-reciprocityTemp6000)
rename reciprocityTemp6000 reciprocityHigh1
rename reciprocityTemp5000 reciprocityHigh2
rename reciprocityTemp1000 reciprocityLow1
rename reciprocityTemp2000 reciprocityLow2
rename reciprocityTemp3000 reciprocityMedium1
rename reciprocityTemp4000 reciprocityMedium2

label var reciprocityLevel "Points return as % possible available, averaged across all amounts"
label var reciprocityHigh1 "Points return as % possible available, for highest possible amount (12000 points available)"
label var reciprocityHigh2 "Points return as % possible available, for 2nd highest possible amount (10000 points available)"
label var reciprocityLow1 "Points return as % possible available,  for lowest possible amount (2000 points available)"
label var reciprocityLow2 "Points return as % possible available,  for 2nd lowest possible amount (4000 points available)"
label var reciprocityMedium1 "Points return as % possible available,  for first middle amount (6000 points available)"
label var reciprocityMedium2 "Points return as % possible available,  for second middle amount (8000 points available)"

/*************************************************************************
* Generate overestimation measures from IQ
*************************************************************************/
gen overestimationMatrix = perceivedCorrectMatrix - IQMatrix
gen overestimationRotation = perceivedCorrectRotation - IQRotation
gen overestimationIQ = overestimationMatrix + overestimationRotation
label var overestimationMatrix "= perceivedCorrectMatrix - IQMatrix"
label var overestimationRotation "= perceivedCorrectRotation - IQRotation"
label var overestimationIQ "= overestimationMatrix + overestimationRotation"

/*************************************************************************
* generate overprecision, overplacement measures from Telephone / Unemployment
* Source: BLS seasonally adjusted unemployment rate, April 2016 (5.0)
*************************************************************************/
//telephone was invented in 1876
//unemployment rate in April 2016 was 5.0

// Rename survey questions as confidence
rename qualitativeOverprecisionUnemp qualitativeConfidenceUnemp
rename qualitativeOverprecisionTele qualitativeConfidenceTele

//bottomcode Telephone invention
replace perceivedTeleInvention = 1798 if perceivedTeleInvention < 1798

//adjust overconfidence measures for accuracy, first telephone
gen accuracyTelephone = abs(perceivedTeleInvention - 1876)
gen accuracyTelephone2 = accuracyTelephone^2
gen accuracyTelephone3 = accuracyTelephone2*accuracyTelephone
gen accuracyTelephone4 = accuracyTelephone2^2
reg qualitativeConfidenceTele accuracyTelephone*
predict qualitativeOverprecisionTele, resid
xtile actualPercentileTele = accuracyTelephone, nq(100)
gen overplacementTele = actualPercentileTele - perceivedPercentileTele
label var qualitativeOverprecisionTele "Residuals regression of qual confidence against accuracy telephone"
label var overplacementTele "Actual-perceived percentile on onemployment"

//adjust overconfidence measures for accuracy, next unemployment
gen accuracyUnemp = abs(perceivedUnemp - 5.0)
gen accuracyUnemp2 = accuracyUnemp^2
gen accuracyUnemp3 = accuracyUnemp2*accuracyUnemp
gen accuracyUnemp4 = accuracyUnemp2^2
reg qualitativeConfidenceUnemp accuracyUnemp*
predict qualitativeOverprecisionUnemp, resid
xtile actualPercentileUnemp = accuracyUnemp, nq(100)
gen overplacementUnemp = actualPercentileUnemp - perceivedPercentileUnemp
label var qualitativeOverprecisionUnemp "Residuals regression of qual confidence against accuracy unemployment"
label var overplacementUnemp "Actual-perceived percentile on onemployment"

drop accuracy* actualPercentile*

/*************************************************************************
* Transform for analysis variables to 0-1 scale
*************************************************************************/
foreach i in  WTA WTP ambiguityAttitude patience riskAversionGain riskAversionLoss riskAversionUrn {
	qui summ `i'1,d
	gen FA`i'1=(`i'1-r(min))/(r(max)-r(min))
	qui summ `i'2,d
	gen FA`i'2=(`i'2-r(min))/(r(max)-r(min))
	local l`i'1 : variable label `i'1
	label var FA`i'1 "For analysis: `i'1 transformed to 0-1"
	local l`i'2 : variable label `i'2
	label var FA`i'2 "For analysis: `i'2 transformed to 0-1"
}

// Abbreviated version of names
gen FAcompoundLotteryAttitude1=compoundLotteryAttitude1
gen FAcompoundLotteryAttitude2=compoundLotteryAttitude2
gen FAriskAversionMixGainLoss1=riskAversionMixGainLoss1
gen FAriskAversionMixGainLoss2=riskAversionMixGainLoss2

// These are variables where the normalization for the foranalysis variables is different - and defined with the main variable above
foreach i in FAriskAversionCRSure FAriskAversionCRLottery FAadvanInequalityAversion FAdisadInequalityAversion FAcompoundLotteryAttitude FAriskAversionMixGainLoss {	
	summ `i'1 `i'2
 	qui summ `i'1,d
 	replace `i'1=(`i'1-r(min))/(r(max)-r(min))
 	qui summ `i'2,d
	replace `i'2=(`i'2-r(min))/(r(max)-r(min))
}	
label var FAriskAversionCRSure1 "For analysis: divide by sure prize (lin. transform) then transform to 0-1"
label var FAriskAversionCRSure2 "For analysis: divide by sure prize (lin. transform) then transform to 0-1"
label var FAriskAversionCRLottery1 "For analysis: divide by EV safe lottery (lin. transform) then transform to 0-1"
label var FAriskAversionCRLottery2 "For analysis: divide by EV safe lottery (lin. transform) then transform to 0-1"
label var FAadvanInequalityAversion1 "For analysis: divide by equal split amount (lin. transform) then transform to 0-1"
label var FAadvanInequalityAversion2 "For analysis: divide by equal split amount (lin. transform) then transform to 0-1"
label var FAdisadInequalityAversion1 "For analysis: divide by equal split amount (lin. transform) then transform to 0-1"
label var FAdisadInequalityAversion2 "For analysis: divide by equal split amount (lin. transform) then transform to 0-1"
label var FAcompoundLotteryAttitude1 "For analysis: compoundLotteryAttitude1 transformed to 0-1"
label var FAcompoundLotteryAttitude2 "For analysis: compoundLotteryAttitude2 transformed to 0-1"
label var FAriskAversionMixGainLoss1 "For analysis: riskAversionMixGainLoss1 transformed to 0-1"
label var FAriskAversionMixGainLoss2 "For analysis: riskAversionMixGainLoss2 transformed to 0-1"

gen FAcommonRatio1=FAriskAversionCRSure1-FAriskAversionCRLottery1
label var FAcommonRatio1 "For analysis: =FAriskAversionCRSure1-FAriskAversionCRLottery1"
gen FAcommonRatio2=FAriskAversionCRSure2-FAriskAversionCRLottery2
label var FAcommonRatio2 "For analysis: =FAriskAversionCRSure2-FAriskAversionCRLottery2"
gen FAcompoundAversion1 = FAcompoundLotteryAttitude1 - FAriskAversionUrn1
label var FAcompoundAversion1 "For analysis: =FAcompoundLotteryAttitude1 - FAriskAversionUrn1"
gen FAcompoundAversion2 = FAcompoundLotteryAttitude2 - FAriskAversionUrn2
label var FAcompoundAversion2 "For analysis: =FAcompoundLotteryAttitude2 - FAriskAversionUrn2"
gen FAambiguityAversion1 = FAambiguityAttitude1 - FAriskAversionUrn1
label var FAambiguityAversion1 "For analysis: =FAambiguityAttitude1 - FAriskAversionUrn1"
gen FAambiguityAversion2 = FAambiguityAttitude2 - FAriskAversionUrn2
label var FAambiguityAversion2 "For analysis: =FAambiguityAttitude2 - FAriskAversionUrn2"
gen FAendowmentEffect1=FAWTA1-FAWTP1
label var  FAendowmentEffect1 "For analysis: =FAWTA1-FAWTP1"
gen FAendowmentEffect2=FAWTA2-FAWTP2
label var FAendowmentEffect2 "For analysis: =FAWTA2-FAWTP2"

// For discount rate, use same as linear time variable 
gen FAdiscountRate1=FApatience1
label var FAdiscountRate1 "For analysis: copy of FApatience1 (since discountRate is nonlinear transform)"
gen FAdiscountRate2=FApatience2
label var FAdiscountRate2 "For analysis: copy of FApatience2 (since discountRate is nonlinear transform)"

// For non-MPL vars, create For Analysis variables that is same as main variable and label the same
foreach var of varlist altruism reciprocity* trust IQ* overestimation* overplacement* qual* CRT strategicSophistication1 strategicSophistication2 punishSender punishReceiver  {
	gen FA`var'=`var'
	local l`var' : variable label `var'
	label var FA`var' "For analysis: copy of `var'"
}


/*************************************************************************
* Generate average variables
*************************************************************************/
foreach i in WTA WTP endowmentEffect ambiguityAttitude compoundAversion ambiguityAversion ///
	patience riskAversionGain  riskAversionUrn riskAversionLoss riskAversionCRSure commonRatio reciprocityHigh reciprocityLow ///
	compoundLotteryAttitude riskAversionMixGainLoss riskAversionCRLottery advanInequalityAversion disadInequalityAversion strategicSophistication {
		gen avg_`i'=(FA`i'1+FA`i'2)/2
		label var avg_`i' "Mean of FA variables of `i'"
}

gen avg_IQ=(IQMatrix+IQRotation)/2	
label var avg_IQ "=(IQMatrix+IQRotation)/2"
gen avg_punishment=(punishSender+punishReceiver)/2	
label var avg_punishment "=(punishSender+punishReceiver)/2"
gen avg_overEstimateIQ=(overestimationMatrix+overestimationRotation)/2	
label var avg_overEstimateIQ "=(overestimationMatrix+overestimationRotation)/2"	
gen avg_overPlacementFacts=(overplacementTele+overplacementUnemp)/2	
label var avg_overPlacementFacts "=(overplacementTele+overplacementUnemp)/2"
gen avg_qualOverPrecise=(qualitativeOverprecisionTele+qualitativeOverprecisionUnemp)/2	
label var avg_qualOverPrecise "=(qualitativeOverprecisionTele+qualitativeOverprecisionUnemp)/2"

/*************************************************************************
* Construct demographic variables
*************************************************************************/
gen age = 2016 - birthyr
gen agesq=age^2
label var age "Age of respondent"
label var agesq "Age of respondent squared"

rename faminc incomeCategorical
label var incomeCategorical "Income in categories, including those not answering"
replace incomeCategorical=12 if incomeCategorical==31 /** A couple of observations that are coded as just $150000 or more. Put into 150000-200000 bin **/
gen incomeLinear = incomeCategorical
replace incomeLinear = . if incomeCategorical == 97
label var incomeLinear "Income for linear analysis, setting no answer to missing"
rename educ education
gen male = gender == 1
label var male  "1 if male, 0 if female"

// Fix messed up labels (messed up in original data from YouGov)
label define income 1 "Less than $10,000" 2 "$10,000 - $19,999" 3 "$20,000 - $29,999" 4 "$30,000 - $39,999" 5 "$40,000 - $49,999" 6 "$50,000 - $59,999" 7 "$60,000 - $69,999" ///
	8 "$70,000 - $79,999" 9 "$80,000 - $99,999" 10 "$100,000 - $119,999" 11 "$120,000 - $149,999" 12 "$150,000 - $199,999" 13 "$200,000 - $249,999" 14 "$250,000 - $349,999" ///
	15 "$350,000 - $499,999" 16 "$500,000 or more" 97 "Prefer not to say" 

label values incomeLinear income
label values incomeCategorical income

// New version of church attendance in ascending order of attendance
gen churchAttendanceCategorical_pdl=7-pew_churatd_pdl
replace churchAttendanceCategorical_pdl=97 if pew_churatd_pdl==7
replace churchAttendanceCategorical_pdl=98 if pew_churatd_pdl==8
replace churchAttendanceCategorical_pdl=99 if pew_churatd_pdl==9
#delimit;
label define churchAttendance_pdl 
	1 "Never" 2 "Seldom" 3 "A few times a year" 4 "Once or twice a month" 
	5 "Once a week" 6 "More than once a week"
	97 "Don't know" 98 "Skipped" 99 "Not Asked" ;
#delimit cr
label values churchAttendanceCategorical_pdl churchAttendance_pdl 
label var churchAttendanceCategorical_pdl "Pew church attendance from YouGov database (reordered from pew_churatd_pdl)"

// "linear" version without don't knows and missings
gen churchAttendanceLinear_pdl=churchAttendanceCategorical_pdl if pew_churatd_pdl<7
label values churchAttendanceLinear_pdl churchAttendance_pdl
label var churchAttendanceLinear_pdl "Pew church attend. from YouGov database excl.missing/dontknow (from pew_churatd_pdl)"

/*************************************************************************
* Keep WTAP CEs
*************************************************************************/
generate ceWTA1=WTA1Temp
label var ceWTA1 "CE for WTA 1 (in points)"
generate ceWTA2=WTA2Temp
label var ceWTA2 "CE for WTA 2 (in points)"
generate ceWTP1=WTP1Temp
label var ceWTP1 "CE for WTP1 (in points)"
generate ceWTP2=WTP2Temp
label var ceWTP2 "CE for WTP 2 (in points)"

/*************************************************************************
* drop variables we don't need
*************************************************************************/
rename riskAversionCRSure1Temp riskAversionCRSure1MidP
rename riskAversionCRSure2Temp riskAversionCRSure2MidP
rename riskAversionCRLottery1Temp riskAversionCRLottery1MidP
rename riskAversionCRLottery2Temp riskAversionCRLottery2MidP
label var riskAversionCRSure1MidP "Midpoint of MPL for riskAversionCRSure1 in points"
label var riskAversionCRSure2MidP "Midpoint of MPL for riskAversionCRSure2 in points"
label var riskAversionCRLottery1MidP "Midpoint of MPL for riskAversionCRLottery1 in points"
label var riskAversionCRLottery2MidP "Midpoint of MPL for riskAversionCRLottery2 in points"
drop *Min *Max  *Temp 
drop  dictator trustSent trustSentBack* perceived* gender birthyr

/*************************************************************************
* save file with transformed values
*************************************************************************/
compress
save "${datapath}econographicsTransformed.dta", replace


/*******************************************************
**Rename some variables for correlation computation
*******************************************************/
use "${datapath}econographicsTransformed.dta", clear
keep caseid weight FA* age male incomeLinear education churchAttendanceLinear_pdl
renvars FA*, presub("FA" "")

** Over-estimation using IQ questions; already on same scale
rename overestimationMatrix overEstimateIQ1
rename overestimationRotation overEstimateIQ2
** Over-placement using Tele/Unempl questions; already on same scale
rename overplacementTele overPlacementFacts1
rename overplacementUnemp overPlacementFacts2	

** Over-placement using Tele/Unempl questions; already on same scale
rename qualitativeOverprecisionTele qualOverPrecise1
rename qualitativeOverprecisionUnemp qualOverPrecise2

**Punishment using punishing sender and receiver
gen punishment1=punishSender 
gen punishment2=punishReceiver

**Extensive margin of punishment
gen PSext = punishSender > 0
gen PRext = punishReceiver > 0

**Dual Reciprocity Level measures
drop reciprocityLevel
gen reciprocityLevel1 = (reciprocityLow1 + reciprocityMedium2 + reciprocityHigh1)/3
gen reciprocityLevel2 = (reciprocityLow2 + reciprocityMedium1 + reciprocityHigh2)/3

save "${datapath}econographicsForPCA.dta", replace

///***********************************************************************
// Create a different version of the survey dataset with the transformed variables 
// to be used to create pca factors
// Also creates a couple of other variables for the paper
// Only needs running again if survey dataset is updated 
// Here it is done for all measures that need it and now commented
///***********************************************************************
// Use main Econographics dataset
use "${datapath}econographicsTransformed.dta", clear

#delimit;
local varlistAllToNormalize "altruism trust patience punishSender punishReceiver strategicSophistication
					reciprocityLow reciprocityHigh advanInequalityAversion disadInequalityAversion 
					WTA riskAversionGain riskAversionLoss riskAversionMixGainLoss 
					WTP riskAversionCRSure riskAversionCRLottery commonRatio endowmentEffect
					ambiguityAversion compoundAversion compoundLotteryAttitude ambiguityAttitude
					overEstimateIQ overPlacementFacts qualOverPrecise";
#delimit cr

** Over-estimation using IQ questions; already on same scale
rename FAoverestimationMatrix FAoverEstimateIQ1
rename FAoverestimationRotation FAoverEstimateIQ2
** Over-placement using Tele/Unempl questions; already on same scale
rename FAoverplacementTele FAoverPlacementFacts1
rename FAoverplacementUnemp FAoverPlacementFacts2	
drop FAoverestimationIQ

** Over-precision using Tele/Unemp questions; already on same scale
rename FAqualitativeOverprecisionTele FAqualOverPrecise1
rename FAqualitativeOverprecisionUnemp FAqualOverPrecise2

// Create a new average version of each variable needed
// Then standardize for use in constructing the factors
foreach i in `varlistAllToNormalize' {
	cap gen  FA`i' = (FA`i'1 + FA`i'2)/2
	summarize  FA`i' [aweight=weight]
	replace FA`i' = (FA`i'-r(mean))/r(sd)
	cap summ FA`i'1 [aweight=weight]
	cap replace FA`i'1 = (FA`i'1-r(mean))/r(sd)
	cap summ FA`i'2 [aweight=weight]
	cap replace FA`i'2 = (FA`i'2-r(mean))/r(sd)
} 

** Over-estimation using IQ questions; already on same scale
rename overestimationMatrix overEstimateIQ1
rename overestimationRotation overEstimateIQ2
** Over-placement using Tele/Unempl questions; already on same scale
rename overplacementTele overPlacementFacts1
rename overplacementUnemp overPlacementFacts2	

** Over-placement using Tele/Unempl questions; already on same scale
rename qualitativeOverprecisionTele qualOverPrecise1
rename qualitativeOverprecisionUnemp qualOverPrecise2

rename IQ IQSum

replace FAstrategicSophistication = -FAstrategicSophistication
replace strategicSophistication1 = -strategicSophistication1
replace strategicSophistication2 = -strategicSophistication2

// Save version with weights to be used below.
save "${datapath}econographicsTransformedWithComponents.dta", replace




